package org.vidi.springamqp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.vidi.springamqp.entity.Order;
import org.vidi.springamqp.producer.OrderProducer;
import org.vidi.springamqp.producer.OrderProducerConfirm;

import java.util.Random;

/**
 * @author vidi
 */
@Controller
@RequestMapping("/rabbitmq")
public class OrderController {
    private final OrderProducer orderProducer;
    private final OrderProducerConfirm orderProducerConfirm;

    public OrderController(OrderProducer orderProducer, OrderProducerConfirm orderProducerConfirm) {
        this.orderProducer = orderProducer;
        this.orderProducerConfirm = orderProducerConfirm;
    }

    @GetMapping("/order/simple/{name}")
    @ResponseBody
    public String simpleOrder(@PathVariable String name) {
        Order order = new Order();
        final int id = new Random().nextInt();
        order.setId(id);
        order.setName(name);
        order.setMessageId(name + id);
        orderProducer.send(order);
        return "Submit successful.";
    }

    @GetMapping("/order/confirm/{name}")
    @ResponseBody
    public String confirmOrder(@PathVariable String name) {
        Order order = new Order();
        final int id = new Random().nextInt();
        order.setId(id);
        order.setName(name);
        order.setMessageId(name + id);
        orderProducerConfirm.send(order);
        return "Submit successful.";
    }
}
